package com.ruoyi.web.controller.tool.xml;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;

public class ExportCbtrihDataToFiles {

    // 数据库配置
    private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "password01!";

    // 输出目录
    private static final String OUTPUT_DIR = "C:/output_files";
    private static final int ROWS_PER_FILE = 10;

    public static void main(String[] args) {
        // 创建输出目录
        java.io.File outputDir = new java.io.File(OUTPUT_DIR);
        if (!outputDir.exists()) {
            outputDir.mkdirs();
        }

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            // 获取总行数
            int totalRows = getTotalRows(conn);

            // 计算需要生成的文件数量
            int fileCount = (totalRows + ROWS_PER_FILE - 1) / ROWS_PER_FILE;

            // 分页查询并导出
            for (int i = 0; i < fileCount; i++) {
                int offset = i * ROWS_PER_FILE;
                String outputFile = OUTPUT_DIR + "/FILENAME.p" + String.format("%03d", (i + 1));

                try (FileWriter writer = new FileWriter(outputFile)) {
                    // 写入文件开头
                    writer.write("|||BEGIN|||\n");

                    // 查询数据
                    String query = "SELECT PRDCDE, IBKNUM, CRNYER, PDNSEQ, REFNUM, SNDBAK, RETABN, RCVBAK, BBKNAM, RETNAM, RETADR, BENNAM, BENADR, RMTUSE, WKTYPE, RMTATY, RMTFLG, RMTOAC, RMTSYS, RMTORG, RMTCUS, RMTAPC, RMTCUR, BENATY, BENFLG, BENOAC, BENSYS, BENORG, BENCUS, BENAPC, BENCUR, RETAMT, ORGIDT, TLRNUM, VCHSET, HDLTLR, RETDAT, RETTIM, CCLDAT, VALDAT, CSHTYP, RTMFLG, FUNCDE, RETREF, ADJDAT, ADJTLR, PNTTIM, RETSTS, RECSTS FROM cbtrih LIMIT ? OFFSET ?";  // 替换为你的表名和字段
                    try (PreparedStatement stmt = conn.prepareStatement(query)) {
                        stmt.setInt(1, ROWS_PER_FILE);
                        stmt.setInt(2, offset);
                        try (ResultSet rs = stmt.executeQuery()) {
                            int rowIndex =  1;
                            while (rs.next()) {
                                // 构造行数据
                                String line = rowIndex + " " +
                                        rs.getString("PRDCDE") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("IBKNUM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("CRNYER") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("PDNSEQ") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("REFNUM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("ORINUM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("SNDBAK") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RETABN") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RCVBAK") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BBKNAM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RETNAM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RETADR") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BENNAM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BENADR") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTUSE") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("WKTYPE") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTATY") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTFLG") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTOAC") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTSYS") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTORG") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTCUS") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTAPC") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTCUR") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BENATY") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BENFLG") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BENOAC") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BENSYS") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BENORG") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BENCUS") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BENAPC") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BENCUR") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FEEATY") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FEEFLG") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FEEOAC") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FEESYS") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FEEORG") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FEECUS") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FEEAPC") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FEECUR") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RETAMT") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("CUSAM1") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTFEE") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("COMFEE") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("MALFEE") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("ORGIDT") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("TLRNUM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("VCHSET") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("DEPNUM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("MODTLR") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("MODDAT") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("HDLGRP") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("HDLDAT") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("HDLTIM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("CHKTLR") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("CHKDAT") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("CHKTIM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("AUTTLR") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("ATHDAT") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("ATHTIM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("VALDAT") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("HANDAT") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("CCLDAT") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FEEMDE") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("CSHTYP") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RATVA1") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RATVA2") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RETREF") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("ANACDE") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("BATFEE") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RTMFLG") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RRMFLG") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FUNCDE") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("PNTTIM") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("ADJDAT") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("ADJTLR") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("ATVORG") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("ATVTLR") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("ATVSET") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RETSTS") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTPTY") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RMTPAP") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FEEPTY") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("FEEPAP") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("RECSTS") + "\u007F" + "|" + "\u007F"+"^ENDROW"+"\n";
                                writer.write(line);
                                rowIndex++;
                            }
                        }
                    }

                    // 写入文件结尾
                    writer.write("|||END|||\n");
                }

                System.out.println("Exported " + outputFile);
            }
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }

    // 获取总行数
    private static int getTotalRows(Connection conn) throws SQLException {
        String query = "SELECT COUNT(*) FROM cbtrih";  // 替换为你的表名
        try (Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(query)) {
            if (rs.next()) {
                return rs.getInt(1);
            }
        }
        return 0;
    }
}